package cn.biplam.front.upload;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.FileDwldOptCode;
import cn.biplam.common.opcode.SysManageCode.FileUploadOptCode;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.struc.SysStruc.FileDownLoadStru;
import cn.biplam.common.struc.SysStruc.FileUpLoadStru;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.Index;
import cn.biplam.front.template.base.FileDownLoad.File_Src;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * 文件下载服务类
 * @author 	WangSong 2009-6-20 下午08:15:37
 * @version 1.0
 */
public class FileDownload extends HttpServlet {
	/** */
	private static final long serialVersionUID = -4049020002255689976L;
	private ServletConfig config;

	/**
	 * 保存Servlet config属性,供SmartUpload使用
	 * @param config
	 * @throws ServletException
	 */
	public void init(ServletConfig config) throws ServletException {
		this.config = config;
	}

	/**
	 * 文件下载服务响应接口
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)	throws ServletException, IOException {
		try{
			String id = request.getParameter("id");
			String type = request.getParameter("type");
			OperatorResult rs = null;
			String sFullPath = null;

			if(File_Src.Download.toString().equals(type)){
				rs = Operator.get(FileDwldOptCode.Query_ByID, null, new DataFilter(id));
				sFullPath=this.getFullPath(rs);
			}else if(File_Src.Upload.toString().equals(type)){
				DataFilter filter = new DataFilter();
				filter.put(FileUpLoadStru.FILE_ID, id);
				/**文件来自upload表*/
				rs = Operator.get(FileUploadOptCode.Query_ByID, null, filter);
				sFullPath=this.getUploadPath(rs);
			}else{
				throw new ErrorMessage("Invalid file source type!");
			}
			SmartUpload mySmartUpload = new SmartUpload();
			mySmartUpload.initialize(config, request, response);
			mySmartUpload.setContentDisposition(null);
			mySmartUpload.downloadFile(sFullPath);
		} catch (ErrorMessage e) {
			/**转发到系统错误页面*/
			Index.printError(request, response, e);
		}catch (SmartUploadException e) {
			ErrorMessage me = new ErrorMessage(e);
			Index.printError(request, response, me);
		}
	}

	/**
	 * 从属性文件和记录集中获取文件全路径名
	 * @param rs	包含文件信息的记录集
	 * @return	String 文件全路径名
	 * @throws ErrorMessage
	 */
	private String getFullPath(OperatorResult rs) throws ErrorMessage{
		StringBuilder sb = new StringBuilder();
		String downloadDir=TOOLS.getValueByPropertyName(SysName.PropertiesPath, "DownloadPath");
		if(downloadDir!=null)
			sb.append(downloadDir);
		if(rs.size()==0){
			throw new ErrorMessage("Template file record is not found!");
		}
		String filePath=rs.getValue(FileDownLoadStru.FILE_PATH);
		String fileName=rs.getValue(FileDownLoadStru.FILE_NAME);
		sb.append(filePath==null?"":filePath);
		sb.append(fileName==null?"":fileName);

		return sb.toString();
	}
	
	/**
	 * 获取上传完文件的绝对路径
	 * @param rs 包含文件信息的记录集
	 * @return   文件全路径名
	 * @throws ErrorMessage
	 */
	private String getUploadPath(OperatorResult rs) throws ErrorMessage{
		StringBuilder sb = new StringBuilder();
		if(rs.size()==0){
			throw new ErrorMessage("Template file record is not found!");
		}
		String filePath=rs.getValue(FileUpLoadStru.FILE_PATH);
		String fileName=rs.getValue(FileUpLoadStru.FILE_NAME);
		sb.append(filePath==null?"":filePath);
		sb.append(fileName==null?"":fileName);

		return sb.toString();
	}
}
